home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / xdme_1.84_src.lha / XDME / Lib / src / AUTO.c next >
Encoding:
C/C++ Source or Header  |  1994-06-15  |  7.3 KB  |  319 lines

  1.  
  2. /******************************************************************************
  3.  
  4.     MODULE
  5.     AUTO.c
  6.  
  7.     DESCRIPTION
  8.     The Purpose of this Module is to add
  9.     __autoinit-abilities to every known
  10.     C-compiler by adding one simple line
  11.     to the top of the main() function.
  12.  
  13.     this Module was created, 'cause we
  14.     often used DICE's __autoinit qualifier
  15.     and we were not able to port XDME to
  16.     any other platform without bigger changes
  17.     without such a construct.
  18.  
  19.     this module exports 1 function:
  20.         void AUTO_Init(void);
  21.     where AUTO_Init() should be called from
  22.     or before main().
  23.     when using DICE or SAS/C that functioncall
  24.     is obsolete, but using GCC it is really
  25.     necessary.
  26.  
  27.  
  28.     USAGE
  29.  
  30.       METHOD 1
  31.  
  32.     that method does (hopefully) not use any compiler
  33.     depencies, to enable auto-initialisation;
  34.     all You need is an ANSI-Compiler, a Make-Utility
  35.     and a Grep-Utility
  36.  
  37.       * call "AUTO_Init();" at the beginning of main()
  38.  
  39.       * when using gcc it is necessary to add
  40.     the following dependencies to Your Makefile:
  41.  
  42.     M>
  43.     M>_AUTO_enex.h : $(SOURCESC)
  44.     M>      egrep -h -e "^[ ]*(MK_)?AUTO(IN|EX)IT[ ]*\(.*\)[ ]*([;/]|$)" $(SOURCESC) > $*.h1
  45.     M>      sed    -e "s/\([;/{].*$\)|\(.*:\)//g" $*.h1 > $*.h
  46.     M>#                    ^--- drop leding file-comments of grep
  47.     M>#            ^--- drop the res of the current line
  48.     M>      rm    $*.h1
  49.     M>
  50.     M>AUTO.o : _AUTO_enex.h
  51.     M>
  52.  
  53.     depending on which system the Makefile is executed on,
  54.     the dependencies can look much nicer or more ugly (I had
  55.     to get to know, that on HPUX the "-h"-option in egrep is
  56.     not available; on AMIGA pipes are officially not supported
  57.     by the root-shell (You might have to replace "rm" by "delete"
  58.     here, too))
  59.  
  60.       * each autoentry/exit-function should get a line
  61.     in its Module's Header of the following form:
  62.  
  63.     for autoentry:
  64.     C> AUTOINIT(<function_name>)
  65.     C>
  66.  
  67.     for autoexit:
  68.     C> AUTOEXIT(<function_name>)
  69.     C>
  70.  
  71.     which might be followed by a comment or a semicolon
  72.     (but to be on the safe side You'd better not put anything
  73.     but Your AUTO..IT(...) definition into that line, since we
  74.     have not totally managed deleting other stuff)
  75.  
  76.  
  77.       METHOD 2
  78.  
  79.     that Method is designed to support the Compiler-builtin
  80.     Auto-initialisation features, like used by DICE or SAS/C,
  81.     but if not using one of those compilers,
  82.  
  83.       * call "AUTO_Init();" at the beginning of main()
  84.     this is done for future support of GNU/C, which to my
  85.     knowledge has no autoinit support
  86.  
  87.       * surround the definition of each auto??it-function
  88.     with MK_AUTO??IT(), where MK_AUTO..IT must be the 1st
  89.     word of a line. Wanna say, write
  90.     C> MK_AUTOINIT(my_init) { ...
  91.     where DICE expects
  92.     C> __autoinit void my_init(void) { ...
  93.     or SAS/C expects
  94.     C> void STI_my_init (void) { ...
  95.  
  96.       * since that method is not ready to use with GNU/C
  97.     there is currently no need to do any addes to the Makefile
  98.     but that might be necessary, if a later version of Method#2
  99.     does also support GNU/C
  100.  
  101.  
  102.  
  103.     NOTES
  104.     Please note these two methods are to be used mutally exclusive, i.e.
  105.     using "AUTO??IT(func_x)" and defining "MK_AUTO??IT(func_x){...}"
  106.     causes func_x be called twice, which might lead into undefined
  107.     situations ...
  108.  
  109.     REQUIREMENTS
  110.  
  111.       METHOD 1
  112.  
  113.       * You must use egrep or something like that to extract
  114.     the autoinit-funktions from Your sources;
  115.     the "-h" options seems not to be available for each version
  116.     of egrep over there (#$&%^$& HPUX), so there might be some
  117.     other solution neccessary.
  118.  
  119.     e.g. it should be possible using sedwith something like:
  120.     M>  sed -n "s/^ *AUTO..IT( *[A-Za-z0-9_]* *)/p" >
  121.     but I was never forced to use something like that,
  122.     so it is untested.
  123.  
  124.       METHOD 2
  125.  
  126.       * while using SAS/C or DICE there is no other tool necessary
  127.     when using GNU/C a parser like shown below is necessary,
  128.     so lex might be used to create it...
  129.  
  130.     the parser might be a lex-pgm like the following
  131.     L> WS    [ \n\t]
  132.     L> DLM    [A-Za-z_][A-Za-z_0-9]*
  133.     L> %%
  134.     L> ^{WS}*(MK_)?AUTO(IN|EX)IT{WS}*"("{WS}*{DLM}{WS}*")" printf("%s;",yytext);
  135.     L> .                          ;
  136.     to create the scanner just type
  137.     %> lex lex.l
  138.     %> gcc lex.yy.c -ll
  139.     to get it work in a Makefile
  140.     M> _AUTO_enex.h : $(OWN_C_SRCS)
  141.     M>       cat $(OWN_C_SRCS) | a.out > _AUTO_ENEX.h
  142.  
  143.     BUGS
  144.     none known
  145.  
  146.     TODO
  147.     tell me
  148.  
  149.     EXAMPLES
  150.  
  151.     SEE ALSO
  152.     AUTO.h
  153.     (DICE: DCC:doc/EXTENSIONS.doc DCC:doc/COMPILER.doc)
  154.  
  155.     INDEX
  156.  
  157.     HISTORY
  158.     05-12-93 b_noll created
  159.     15-06-94 b_noll added (void) into MK_AUTO??IT
  160.  
  161. ******************************************************************************/
  162.  
  163.  
  164. /**************************************
  165.           Includes
  166. **************************************/
  167.  
  168.  
  169. #ifndef   AUTO_H
  170. #include "AUTO.h"
  171. #endif /* AUTO_H */
  172.  
  173. #ifndef   STDLIB_H
  174. #include <stdlib.h>
  175. #endif /* STDLIB_H */
  176.  
  177. /**************************************
  178.         Global Variables
  179. **************************************/
  180.  
  181. /* none */
  182.  
  183. /**************************************
  184.       Internal Defines & Structures
  185. **************************************/
  186.  
  187. #undef      AUTOINIT
  188. #undef      AUTOEXIT
  189. #undef MK_AUTOEXIT
  190. #undef      AUTO_Init
  191.  
  192.  
  193. /* ---- when using SAS/C or DICE we must ignore method # 2 */
  194. #if SUPPORT_AUTO
  195. #define CALL2(x)
  196. #else
  197. #define CALL2(x) x();
  198. #endif
  199.  
  200. /**************************************
  201.        Internal Variables
  202. **************************************/
  203.  
  204. /* none */
  205.  
  206. /**************************************
  207.        Internal Prototypes
  208. **************************************/
  209.  
  210.  
  211. extern void AUTO_Init(void);
  212. extern void AUTO_Exit(void);
  213.  
  214. /**************************************
  215.          Macros
  216. **************************************/
  217.  
  218. /* see everywhere we include "_AUTO_enex.h" */
  219.  
  220. /**************************************
  221.          Implementation
  222. **************************************/
  223.  
  224.  
  225. ; MK_AUTOINIT( AUTO_Init ) /*
  226. ^--------------------------------- with that semicolon we disable the sanner  */
  227. {                /* for that occurancy of MK_AUTOINIT          */
  228.  
  229. /* ---- *AUTOEXIT is ignored in that function */
  230. #define       AUTOEXIT(x)
  231. #define    MK_AUTOEXIT(x)
  232. #undef       MK_AUTOINIT
  233.  
  234.  
  235.     /* ---- declare protos */
  236. #   define    AUTOINIT(x) extern void x(void);
  237. #   define MK_AUTOINIT(x) extern void x(void);
  238. #include    "_AUTO_enex.h"
  239. #   undef  MK_AUTOINIT
  240. #   undef     AUTOINIT
  241.  
  242.  
  243.     /* ---- be sure we call AUTO_Exit */
  244.     atexit (AUTO_Exit);
  245.  
  246.  
  247.     /* ---- call the functions */
  248. #   define MK_AUTOINIT(x) CALL2(x)
  249. #   define    AUTOINIT(x) x();
  250. #include    "_AUTO_enex.h"
  251. #   undef     AUTOINIT
  252. #   undef  MK_AUTOINIT
  253.  
  254.  
  255. #undef       AUTOEXIT
  256. #undef    MK_AUTOEXIT
  257. } /* AUTO_Init */
  258.  
  259.  
  260.  
  261.  
  262.  
  263. void AUTO_Exit (void)
  264. {
  265.  
  266. /* ---- *AUTOINIT is ignored in that function */
  267. #define       AUTOINIT(x)
  268. #define    MK_AUTOINIT(x)
  269.  
  270.  
  271.     /* ---- declare protos */
  272. #   define    AUTOEXIT(x) extern void x(void);
  273. #   define MK_AUTOEXIT(x) extern void x(void);
  274. #include    "_AUTO_enex.h"
  275. #   undef  MK_AUTOEXIT
  276. #   undef     AUTOEXIT
  277.  
  278.  
  279.     /* ---- call the functions */
  280. #   define MK_AUTOEXIT(x) CALL2(x)
  281. #   define    AUTOEXIT(x) x();
  282. #include    "_AUTO_enex.h"
  283. #   undef     AUTOEXIT
  284. #   undef  MK_AUTOEXIT
  285.  
  286.  
  287. #undef       AUTOINIT
  288. #undef    MK_AUTOINIT
  289. } /* AUTO_Exit */
  290.  
  291. /******************************************************************************
  292. *****  END AUTO.c
  293. ******************************************************************************/
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301. /* Hier noch einmal das lex-pgm fuer BEIDE Methoden; um */
  302. /* Methode X ausschliesslich zu parsen, muss bei "SMB"  */
  303. /* fuer #2 das "?" und fuer #1 "(MK_)?" entfernt weden  */
  304.  
  305.  
  306. #ifdef LEX
  307.  
  308. WS   [ \n\t]
  309. DLM  [A-Za-z_][A-Za-z_0-9]*
  310. SMB  (MK_)?AUTO(IN|EX)IT
  311.  
  312. %%
  313.  
  314. ^{WS}*{SMB}{WS}*"("{WS}*{DLM}{WS}*")" ECHO;
  315. .                      ;
  316.  
  317. #endif /* LEX */
  318.  
  319.